//
//  AFNetWorkingTool.m
//  SunshineMusic
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 G.Group. All rights reserved.
//

#import "AFNetWorkingTool.h"
#import <AFNetworking.h>

@implementation AFNetWorkingTool

+ (void)getNetWithUrl:(NSString *)url body:(id)body headFile:(NSDictionary *)headFile responseStyle:(GHJResponseStyle)responseStyle success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    //1,创建网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2,请求头的添加
    if (headFile) {
        for (NSString *key in headFile.allKeys) {
            [manager.requestSerializer setValue:headFile[key] forHTTPHeaderField:key];
        }
    }
    //3,返回数据的类型
    switch (responseStyle) {
        case GHJJson:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case GHJXml:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case GHJData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    //4,响应返回数据类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"application/x-javascript",@"image/jpeg", nil]];
    //5,UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //6,发送请求
       [manager GET:url parameters:body progress:^(NSProgress * _Nonnull downloadProgress) {
           
           
       } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           
           if (responseObject) {
               success (task, responseObject);
           }
           
       } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
           
           if (error) {
               failure (task, error);
               NSLog(@"%@", error);
           }
       }];
}
+ (void)postNetWithUrl:(NSString *)url
                  body:(id)body
             bodyStyle:(GHJRequestStyle)bodyStyle
              headFile:(NSDictionary *)headFile
         responseStyle:(GHJResponseStyle)responseStyle
               success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
               failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    //1,创建网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2, body类型
    switch (bodyStyle) {
        case GHJRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case GHJRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                
                return parameters;
            }];
            break;
        default:
            break;
    }
    //3,请求头的添加
    if (headFile) {
        for (NSString *key in headFile.allKeys) {
            [manager.requestSerializer setValue:headFile[key] forHTTPHeaderField:key];
        }
    }
    //4,返回数据的类型
    switch (responseStyle) {
        case GHJJson:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case GHJXml:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case GHJData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    //5,响应返回数据类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"application/x-javascript",@"image/jpeg", nil]];
    //6,UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //7,发送请求
    [manager POST:url parameters:body progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            success (task, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure (task, error);
            NSLog(@"%@", error);
        }

        
    }];

    
}

@end
